package cn.stard.basic.controller;

import cn.stard.basic.dto.SmsCodeDot;
import cn.stard.basic.exception.BusinessException;
 import cn.stard.basic.service.VerifyCodeService;
import cn.stard.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.io.IOException;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {



    @Autowired
private VerifyCodeService verifyCodeService;
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult graph(@PathVariable("imageCodeKey") String imageCodeKey){
        try {
            String imageBase64Str = verifyCodeService.getImageCode(imageCodeKey);
            return AjaxResult.me().setResultObj(imageBase64Str);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
        /*
        * @Title:
        * @Description: 定义一个手机验证码接口
        * @Author: Mr.She
        * @Version: 1.0
        * @Date:
        * @Parameters:
        * @Return
        */

    @PostMapping("/smsCode")
    public AjaxResult upload(@RequestBody SmsCodeDot smsCodeDot){
        try {
                /*调用业务层方法发送短信验证码*/
            verifyCodeService.smsCode(smsCodeDot);
            return AjaxResult.me() ;
            /*设置一个运行时异常来给判断出错的地方回馈到前端*/
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙请重试");

        }
    }

}
